
//
//  MyViewController.m
//  XWZ_News_Work
//
//  Created by penngshi on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyViewController.h"
#import "UserTableViewCell.h"
#import "AboutOurViewController.h"
#import "DateViewController.h"
#import "QRCodeReaderViewController.h"
#import "MyWebViewController.h"
@interface MyViewController ()<UITableViewDataSource , UITableViewDelegate, QRCodeReaderDelegate>
@property (nonatomic ,strong)UIImageView *userImageV;

@property (nonatomic ,strong)UIButton *userButton;

@property (nonatomic ,strong)UITableView *myTableView;

@property (nonatomic ,strong)UIScrollView *bigScrollerV;

@property (nonatomic, retain)QRCodeReaderViewController *reader;


@property (nonatomic, strong)UISwitch *switchButton;
@end

@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithWhite:0.816 alpha:1.000];
    
#pragma mark - 创建一个大的scrollerView
    self.bigScrollerV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 667 * SIZE)];
    self.bigScrollerV.contentSize = CGSizeMake(0, 680 * SIZE);
    self.bigScrollerV.delegate =self;
#pragma mark - 是否显示垂直滚动条
    self.bigScrollerV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.bigScrollerV];
    
   
#pragma mark - 创建最上面大图片
    self.userImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"userBack"]];
    self.userImageV.frame = CGRectMake(0, -20 * SIZE, self.view.frame.size.width, 280 * SIZE);
    self.userImageV.backgroundColor = [UIColor colorWithRed:0.722 green:0.090 blue:0.133 alpha:1.000];
    [self.bigScrollerV addSubview:self.userImageV];
    

    
#pragma mark - 创建一个高度为1的View
    
    UIView *oneView = [[UIView alloc] initWithFrame:CGRectMake(0, 280 * SIZE, 375 * SIZE, 2 * SIZE)];
    oneView.backgroundColor = [UIColor colorWithRed:0.737 green:0.733 blue:0.757 alpha:1.000];
    [self.bigScrollerV addSubview:oneView];
    
#pragma mark - 创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 260 * SIZE, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.scrollEnabled = NO;
    [self.bigScrollerV addSubview:self.myTableView];
    
#pragma mark - 注册
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

#pragma mark - 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    /* 我的界面里的title数组 */
    NSArray *titleArray = [NSArray arrayWithObjects:@"关于我们",@"清除缓存",@"活动广场",@"二 维 码",@"夜间模式", nil];
    /* 我的界面里的title图片数组 */
    NSArray *imageArray = [NSArray arrayWithObjects:@"iconfont-guanyuwomen.png", @"iconfont-qingchuhuancun.png", @"iconfont-guangchang.png", @"iconfont-erweima.png", @"iconfont-yejianmoshi.png",nil];
    
    if (indexPath.section == 0) {
        
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
    }else if(indexPath.section == 1) {
        
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row + 1];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row + 1]];
        
    }else {
        
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row + 3];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row + 3]];
        
    }
    
    
    if (indexPath.section == 2 && indexPath.row == 1) {
        
        self.switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60 * WIDTH, 8 * HEIGHT, 10 * WIDTH, 10 * HEIGHT)];
        self.switchButton.on = [[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"];
        [self.switchButton addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventValueChanged];
        
       
        [cell addSubview:self.switchButton];
    }
    
    return cell;
}
- (void)nightAction:(UISwitch *)sender
{
    //将开关状态存到本地
    [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:@"isNight"];
    
    NSString *command = nil;
    if (YES == sender.on) {
        command = @"夜间模式";
    } else {
        command = @"日间模式";
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:command object:nil];
}

#pragma mark - 必要的方法
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 1;
    }if (section == 1) {
        
        return 2;
    }
    else{
        
        return 2;
    }
    
}

#pragma mark - 有几个分区
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}


#pragma mark - 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

#pragma mark - header
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 9;
}

#pragma mark - footer
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消点击至灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        //关于我们的点击跳转
        AboutOurViewController *aboutVC = [[AboutOurViewController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
#pragma mark - 创建UIAlertController
    if (indexPath.section == 1 && indexPath.row == 0) {
        
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        float cacheSize = size / 1024.0 / 1024.0;
        // 提示框
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存大小:%.2fMB",cacheSize]  preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        
        
        // 创建确定按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //图片缓存清理
            [[SDImageCache sharedImageCache] cleanDisk];
            
            NSLog(@"缓存大小为%.2f",cacheSize);
            [[SDImageCache sharedImageCache]clearDisk];
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
        
        // 创建取消按钮
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        
        [alertC addAction:action2];
        
    }
    
    if (indexPath.section == 1 && indexPath.row == 1)
    {
       DateViewController  *webVC = [[DateViewController alloc] init];

        [self.navigationController pushViewController:webVC animated:YES];
    }

#pragma mark --- 二维码
    if (indexPath.section == 2 && indexPath.row == 0)
    {
        NSArray *types = @[AVMetadataObjectTypeQRCode];
        self.reader = [QRCodeReaderViewController readerWithMetadataObjectTypes:types];
        
        //
        self.reader.delegate = self;
        
        //
        [self.reader setCompletionWithBlock:^(NSString *resultAsString) {
            [self dismissViewControllerAnimated:YES completion:^{
                NSLog(@"----------------%@", resultAsString);
                
                if (!resultAsString.length == 0) {
                    MyWebViewController *web = [[MyWebViewController alloc]init];
                    web.url = resultAsString;
                    [self.navigationController pushViewController:web animated:YES];
                }
                
            }];
        }];
        
        [self presentViewController:self.reader animated:YES completion:NULL];
        
        
    }
    
    
//#pragma mark --- 邮箱反馈
//    if (indexPath.section == 2 && indexPath.row == 1)
//    {
//        //        WJYEmailViewController *emailVC = [[WJYEmailViewController alloc] init];
//        //
//        //        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:emailVC];
//        //
//        //        [self presentViewController:naVC animated:YES completion:^{
//        //
//        //
//        //        }];
//        
//        [self mailButtonAction];
//        
//    }
}


#pragma mark - QRCodeReader Delegate Methods

- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"%@", result);
        
    }];
}

- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

//- (void)changeNavigationBarColor
//{
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.886 green:0.227 blue:0.255 alpha:1];
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
